import 'package:changxindianzishangwu/dao/login_init_dao.dart';
import 'package:changxindianzishangwu/dao/sms_code_dao.dart';
import 'package:changxindianzishangwu/http/base/http_urls.dart';
import 'package:changxindianzishangwu/util/GenerateTestUserSig.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/config.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/util/universal_view.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class PasswordLoginPage extends StatefulWidget {
  PasswordLoginPage({Key key}) : super(key: key);

  @override
  _PasswordLoginPageState createState() => _PasswordLoginPageState();
}

class _PasswordLoginPageState extends State<PasswordLoginPage> {
  var password;
  String phone = Get.arguments["telephone"];

  _getSMS() async {
    await SmsCodeDao.getCode(
        Get.arguments["telephone"], Get.arguments["areaCode"], SIMPLE);
  }

  _passwordLogin(passwordLogin) async {
    //todo 此处进入需要判断是否登录成功
    await LoginInitDao.login(Get.arguments["telephone"], passwordLogin);
    _onLogin();
  }

  //todo 同上 如果上面不成功则不执行 这个方法
  _onLogin() async {
    GenerateTestUserSig usersig = new GenerateTestUserSig(
      sdkappid:  HttpUrls.imSdkAppId(),
      key: HttpUrls.key,
    );
    // String pwdStr = usersig.genSig(identifier: UserId, expire: 86400);
    // EcCache.getInstance().setString("userId", UserId);
  }
  bool passwordVisible1 = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopAppBar(context, "登录/注册"),
      resizeToAvoidBottomInset: false,
      body: GestureDetector(
        onTap: () {
          //收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          color: Colors.white,
          padding: EdgeInsets.all(width(15)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              SizedBox(
                height: height(45),
              ),
              Text(
                "请输入密码",
                style: TextStyle(
                  fontSize: sp(20),
                  fontFamily: "PingFang SC",
                  color: Color.fromRGBO(51, 51, 51, 1),
                ),
              ),
              SizedBox(
                height: height(10),
              ),
              Text(
                "请输入6-18位密码",
                style: TextStyle(
                  fontSize: sp(14),
                  fontFamily: "PingFang SC",
                  color: Color.fromRGBO(153, 153, 153, 1),
                ),
              ),
              SizedBox(
                height: height(30),
              ),
              Text(
                "密码",
                style: TextStyle(
                  fontSize: sp(15),
                  fontFamily: "PingFang SC",
                  color: Color.fromRGBO(85, 85, 85, 1),
                ),
              ),
              Container(
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: TextField(
                        onChanged: (text) {
                          print(text);
                          password = text;
                        },
                        keyboardType: TextInputType.visiblePassword,
                        cursorColor: Colors.greenAccent,
                        obscureText: passwordVisible1,
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "请输入",
                          hintStyle: TextStyle(
                            fontSize: sp(16),
                            color: Color.fromRGBO(153, 153, 153, 1),
                          ),
                            suffixIcon: IconButton(
                                onPressed: () {
                                  setState(() {
                                    passwordVisible1 = !passwordVisible1;
                                  });
                                },
                                icon: Image.asset(
                                  //根据passwordVisible状态显示不同的图标
                                  passwordVisible1
                                      ? "images/ic_n_see_pwd.png"
                                      : "images/ic_see_pwd.png",
                                  width: width(15),
                                  height: width(15),
                                ))
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              UniversalView().line(),
              InkWell(
                onTap: () {
                  if (password == null || password == "") {
                    showToast("密码不能为空");
                  } else if (password != null &&
                      password.length >= 6 &&
                      password.length <= 18) {
                    _passwordLogin(password);

                    //_onLogin(phone);
                    // showToast("登录成功");
                  } else if (password != null && password.length < 6) {
                    showToast("密码太短了");
                  } else if (password != null && password.length > 18) {
                    showToast("密码太长了");
                  }
                  print("登录");
                },
                child: Container(
                  height: ScreenUtil().setHeight(44),
                  width: ScreenUtil().setWidth(300),
                  margin: EdgeInsets.only(
                      right: ScreenUtil().setWidth(75 / 2),
                      left: ScreenUtil().setWidth(75 / 2),
                      top: ScreenUtil().setHeight(60)),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(0, 220, 169, 1),
                      borderRadius:
                          BorderRadius.circular(ScreenUtil().radius(5))),
                  child: Text(
                    "登录",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(15),
                        color: Colors.white,
                        fontFamily: "PingFang SC"),
                  ),
                ),
              ),
              InkWell(
                onTap: () {
                  print(Get.arguments["areaCode"]);
                  Get.toNamed("/getMessagePage", arguments: {
                    "telephone": Get.arguments["telephone"],
                    "areaCode": Get.arguments["areaCode"],
                    "flag": true
                  });
                  _getSMS();

                  print("验证码登录");
                },
                child: Container(
                  alignment: Alignment.center,
                  height: ScreenUtil().setHeight(44),
                  width: ScreenUtil().setWidth(300),
                  margin: EdgeInsets.only(
                      right: ScreenUtil().setWidth(75 / 2),
                      left: ScreenUtil().setWidth(75 / 2),
                      top: ScreenUtil().setHeight(25)),
                  child: Text(
                    "验证码登录",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(13),
                        color: Color.fromRGBO(153, 153, 153, 1),
                        fontFamily: "PingFang SC"),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
